#pragma once
#include "RBTree.hpp"

namespace yiwei
{
	template<class K,class V>
	class map
	{
		
		struct MapKeyOfT
		{
			const K& operator()(const std::pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename RBTree<K, std::pair<K, V>, MapKeyOfT>::iterator iterator;
		typedef typename RBTree<K, std::pair<K, V>, MapKeyOfT>::const_iterator const_iterator;

		map(){}

		map(const map& m)
			:_t(m._t)
		{}

		map& operator=(map m)
		{
			std::swap(_t, m._t);
			return *this;
		}

		std::pair<iterator,bool> insert(const std::pair<K,V>& kv)
		{
			return _t.Insert(kv);
		}

		void erase(iterator pos)
		{
			_t.Erase(pos);
		}

		iterator find(const K& key)
		{
			return _t.Find(key);
		}

		void clear()
		{
			_t.Clear();
		}

		iterator begin()
		{
			return _t.begin();
		}
		iterator end()
		{
			return _t.end();
		}

		V& operator[](const K& key)
		{
			std::pair<iterator, bool> ret = this->insert(std::make_pair(key, V()));
			return ret.first->second;
		}

		bool IsValidRBTree()
		{
			return _t.IsValidRBTree();
		}

	private:
		RBTree<const K, std::pair<K, V>, MapKeyOfT> _t;
	};
}